/**
 *  Copyright 2009-2014 by WWW.KNET.CN  All rights reserved.
 */
package com.domain.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.domain.model.CloudWebDish;
import com.domain.model.CloudWebMessage;
import com.domain.model.Page;
import com.domain.service.CloudWebDishService;
import com.domain.service.CloudWebMessageService;

/**
 * TODO()
 * 
 * @author liujie
 * @date 2015年4月17日 下午1:28:38
 * 
 */
@Controller
@RequestMapping("/dish")
public class DishController extends SuperController {
	private static final Logger logger = LoggerFactory.getLogger(DishController.class);
	@Resource
	private CloudWebDishService cloudWebDishService;
	@Resource
	private CloudWebMessageService cloudWebMessageService;
    @RequestMapping("/list/{type}.html")
	public String dishList(HttpServletRequest request,@PathVariable String type, String porCode,
			Page page,HttpServletResponse response,ModelMap model){
    	Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebDish webDish = new CloudWebDish();
		webDish.setPorCode(porCode);
		webDish.setIfDelete(0l);
		paramMap.put(webDish.key, webDish);
		paramMap.put(PAGE_NAME, page.getPage());
		paramMap.put(PAGESIZE_NAME,page.getPageSize());
		paramMap.put("orderBy", "dish_status desc,dish_when desc");
		List<CloudWebDish> dishList = cloudWebDishService.getListPage(paramMap);
		Long total = cloudWebDishService.getRows(paramMap);
		Page page2 = new Page(page.getPage(),page.getPageSize(), Integer.parseInt(total + ""));
		model.put("list", dishList);
		model.put("page", page2);
		reqInfo(model, porCode, type);
		return String.format("dish/%sList",type);
	}
	@RequestMapping("/dishShowDetail/{id}.html")
	public String dishShowDetail(HttpServletRequest request, @PathVariable Long id, HttpServletResponse response,
			ModelMap model) {
		CloudWebDish dish = cloudWebDishService.get(id);
		model.addAttribute("detail", dish);
		model.addAttribute("porCode",dish.getPorCode());
		logger.info("------------------------dish detail id:{}", id);
		return "dish/detail";
	}
	//各店地址
	@RequestMapping("/storeRoll.html")
	public String storeRoll(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		String porCode="6101";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebDish webDish = new CloudWebDish();
		webDish.setPorCode(porCode);
		webDish.setIfDelete(0l);
		paramMap.put(webDish.key, webDish);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,Integer.MAX_VALUE);
		paramMap.put("orderBy", "dish_status desc,dish_when desc");
		List<CloudWebDish> dishList = cloudWebDishService.getListPage(paramMap);
		model.addAttribute("storeList", dishList);
		 //联系我们
		 CloudWebMessage msg=cloudWebMessageService.get(32l);
		 model.addAttribute("detail", msg);
		return "public/storeRoll";
	}
	/**
	 * 
	 *  TODO地图
	 * @author liujie
	 * @date 2015年6月8日 下午5:37:54
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("/map/{porCode}.html")
	public String mapBd(HttpServletRequest request, @PathVariable String porCode, Long id,
			HttpServletResponse response, ModelMap model) {
		CloudWebDish dish = cloudWebDishService.get(id);
		model.put("entity", dish);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebDish webDish = new CloudWebDish();
		webDish.setPorCode(porCode);
		webDish.setIfDelete(0l);
		paramMap.put(webDish.key, webDish);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,Integer.MAX_VALUE);
		paramMap.put("orderBy", "dish_status desc,dish_when desc");
		List<CloudWebDish> dishList = cloudWebDishService.getListPage(paramMap);
		List<Map<String, Object>> mapList=new ArrayList<Map<String, Object>>();
		for (CloudWebDish obj : dishList) {
			Map<String, Object> m=new HashMap<String, Object>();
			m.put("dishName", obj.getDishName());
			m.put("dishSource", obj.getDishSource());
			m.put("dishUrl", obj.getDishUrl());
			mapList.add(m);
		}
		String array = JSONArray.toJSONString(mapList);
		model.put("mapList",array);
		return "public/map";
	}

}
